package com.mall.mapper;

import com.mall.pojo.Order;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 订单Mapper接口
 */
@Mapper
public interface OrderMapper {
    
    /**
     * 插入订单
     */
    @Insert("INSERT INTO orders (user_id, state, payMoney, postFee, create_time, countdown) " +
            "VALUES (#{userId}, #{state}, #{payMoney}, #{postFee}, #{createTime}, #{countdown})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Order order);
    
    /**
     * 根据ID查询订单
     */
    @Select("SELECT * FROM orders WHERE id = #{id}")
    Order selectById(@Param("id") Long id);
    
    /**
     * 根据用户ID查询订单列表（分页）
     */
    @Select("SELECT * FROM orders WHERE user_id = #{userId} ORDER BY create_time DESC LIMIT #{offset}, #{pageSize}")
    List<Order> selectByUserId(@Param("userId") Long userId, @Param("offset") int offset, @Param("pageSize") int pageSize);
    
    /**
     * 根据用户ID和订单状态查询订单列表（分页）
     */
    @Select("SELECT * FROM orders WHERE user_id = #{userId} AND state = #{state} ORDER BY create_time DESC LIMIT #{offset}, #{pageSize}")
    List<Order> selectByUserIdAndState(@Param("userId") Long userId, @Param("state") int state, @Param("offset") int offset, @Param("pageSize") int pageSize);
    
    /**
     * 统计用户订单总数
     */
    @Select("SELECT COUNT(*) FROM orders WHERE user_id = #{userId}")
    int countByUserId(@Param("userId") Long userId);
    
    /**
     * 统计用户特定状态订单总数
     */
    @Select("SELECT COUNT(*) FROM orders WHERE user_id = #{userId} AND state = #{state}")
    int countByUserIdAndState(@Param("userId") Long userId, @Param("state") int state);
    
    /**
     * 更新订单状态
     */
    @Update("UPDATE orders SET state = #{state} WHERE id = #{id}")
    int updateState(@Param("id") Long id, @Param("state") int state);
    
    /**
     * 逻辑删除订单
     */
    @Delete("DELETE FROM orders WHERE id = #{id}")
    int deleteById(@Param("id") Long id);
} 